// map/reduce/flat/mapfalt/sum
// isArray/shuffle
// 排序/去重


Array.prototype.myReduce = function(cb, initval){
    let res;
    let index = 0;
    if(initval === undefined){
        res = this[index]
        index++
    } else {
        res = initval
    }

    for(let i = index; i< this.length; i++){
        res = cb.call(this, res, this[i], i)
    }
    return res;
}
console.log([1,2,3].myReduce((pre,cur) => (pre*1+cur* 1), 1))

Array.prototype.myFlat = function(dep){
    let res = []
    for(let i = 0; i< this.length; i++){
        if(Array.isArray(this[i]) && dep >= 1){
            let arr = this[i].myFlat(dep - 1);
            res = res.concat(arr)
        } else {
            res.push(this[i])
        }
    }
    return res;

}
console.log([1,1,[2,2,[3,3]]].myFlat(1))

Array.prototype.myShuffle = function(){
    return this.sort((a, b) => Math.random() - 0.5)
}
console.log([1,2,3,4].myShuffle())


Array.isMyArray = function(obj){
    return Object.prototype.toString.call(this, obj) === '[Object Array]'
}
console.log(Array.isMyArray([]))



